﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Collections;
using Telerik.Web.UI;
using System.Drawing;

namespace BVVD_Website.ql_hoc_tap
{
    public partial class frmLopHocvien2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {
                BVVDDataContext db = new BVVDDataContext();

                var listLOPHOC = (from lh in db.LST_LOPHOCs
                                  select lh).ToList();

                ddlLOPHOC.DataSource = listLOPHOC;
                ddlLOPHOC.DataTextField = "TENLH";
                ddlLOPHOC.DataValueField = "MALH";
                ddlLOPHOC.DataBind();
                grvLopHocvien.DataSource = getData();
                grvLopHocvien.DataBind();

            }
        }

        private List<F_LOP_HOCVIEN> getData()
        {
            BVVDDataStore<F_LOP_HOCVIEN> db = new BVVDDataStore<F_LOP_HOCVIEN>();
            Hashtable condition = new Hashtable();
            condition.Add("MALH", ddlLOPHOC.SelectedValue);
            return db.getFromDB(ddlLOPHOC.SelectedIndex >= 0 ? condition : new Hashtable());


        }
        protected void grvLopHocvien_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            grvLopHocvien.DataSource = getData();
        }
        protected void ddlLOPHOC_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {
            grvLopHocvien.DataSource = getData();
            grvLopHocvien.DataBind();
        }
        protected void grvLopHocvien_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {

            BVVDDataContext db = new BVVDDataContext();
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
            RadListBox lboxTOLOP = insertedItem.FindControl("lboxTOLOP") as RadListBox;
            
            List<F_LOP_HOCVIEN> listInsert = new List<F_LOP_HOCVIEN>();

            if (lboxTOLOP.Items.Count > 0)
            {

                foreach (RadListBoxItem item in lboxTOLOP.Items)
                {
                    if (!item.Value.Equals("1"))
                    {
                        F_LOP_HOCVIEN fhv = new F_LOP_HOCVIEN
                        {
                            MALH = ddlLOPHOC.SelectedValue,
                            MAHV = item.Value,
                            TRANGTHAI = "1"
                            
                        };
                        if (checkHVExistInClass(fhv))
                        {
                            listInsert.Add(fhv);
                        }
                    }
                }
            }
            db.F_LOP_HOCVIENs.InsertAllOnSubmit(listInsert);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = listInsert.Count > 0 ? "Thêm mới " + listInsert.Count + " học viên vào lớp " + ddlLOPHOC.SelectedText + " thành công " : "Không có học viên nào được thêm vào lớp " + ddlLOPHOC.SelectedText;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra \n" + ex.Message;

            }


            rnAlert.Show();


        }
        protected void grvLopHocvien_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {


            if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
            {

                BVVDDataContext db = new BVVDDataContext();
                var list = (from l in db.LST_HOCVIENs
                            select new
                            {
                                NAME = l.HODEM + " " + l.TENHV + " | " + l.MAHV,
                                MAHV = l.MAHV,
                                FULLNAME = l.HODEM + " " + l.TENHV,
                                NGAYSINH = l.NGAYSINH
                            }).ToList();

                GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
                RadListBox lboxHOCVIEN = insertedItem.FindControl("lboxHOCVIEN") as RadListBox;
                Label lblHV = insertedItem.FindControl("lblHV") as Label;





                if (ddlLOPHOC.SelectedIndex >= 0)
                {
                    lblHV.Text = "Danh sách học viên hiện tại ở lớp " + ddlLOPHOC.SelectedText;
                    lboxHOCVIEN.DataSource = list;
                    lboxHOCVIEN.DataTextField = "NAME";
                    lboxHOCVIEN.DataValueField = "MAHV";
                    lboxHOCVIEN.DataBind();







                    RadListBox lboxTOLOP = insertedItem.FindControl("lboxTOLOP") as RadListBox;
                    var target = (from hv in db.LST_HOCVIENs
                                  join lp in db.F_LOP_HOCVIENs on hv.MAHV equals lp.MAHV
                                  where lp.MALH.Equals(ddlLOPHOC.SelectedValue)
                                  select new
                                  {
                                      NAME = hv.HODEM + " " + hv.TENHV + " | " + hv.MAHV,
                                      MAHV = hv.MAHV

                                  }).ToList();
                    lboxTOLOP.DataSource = target;
                    lboxTOLOP.DataTextField = "NAME";
                    lboxTOLOP.DataValueField = "MAHV";
                    lboxTOLOP.DataBind();
                }
                else
                {
                    lblHV.Text = "<a href='/ql-hoc-tap/frmLopHocvien.aspx'>Quay lại để chọn lớp học</a>";

                }





            }



        }

        private bool checkHVExistInClass(F_LOP_HOCVIEN obj)
        {
            BVVDDataContext db = new BVVDDataContext();

            bool check = (from hv in db.F_LOP_HOCVIENs
                          where hv.MAHV.Equals(obj.MAHV)
                          && hv.MALH.Equals(obj.MALH)
                          select hv).ToList().Count > 0;

            return !check;

        }


        protected void lboxHOCVIEN_Transferring(object sender, RadListBoxTransferringEventArgs e)
        {
            e.Cancel = true;
            if (e.DestinationListBox.Items.Contains(e.SourceListBox.SelectedItem))
            {
                e.Cancel = true;

            }
        }

        protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();
            var list = (from l in db.LST_HOCVIENs
                        
                        select new
                        {
                            NAME = l.HODEM + " " + l.TENHV + " " + l.MAHV + " " + l.NGAYSINH,
                            MAHV = l.MAHV,
                            FULLNAME = l.HODEM + " " + l.TENHV,
                            NGAYSINH = l.NGAYSINH
                        }).ToList();
            e.Result = list;
        }

        protected void RadSearchBox1_Search(object sender, SearchBoxEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();


            RadSearchBox searchBox = (RadSearchBox)sender;
            RadListBox rad = searchBox.NamingContainer.FindControl("lboxHOCVIEN") as RadListBox;

            if (e.DataItem != null)
            {
                rad.DataSource = (from l in db.LST_HOCVIENs
                                  where (l.MAHV).Contains(((Dictionary<string, object>)e.DataItem)["MAHV"].ToString())
                                  select new
                                  {
                                      NAME = l.HODEM + " " + l.TENHV + " | " + l.MAHV,
                                      MAHV = l.MAHV

                                  }).ToList();


            }
            else
            {
                //(searchBox.NamingContainer.FindControl("lblTEXT") as Label).Text = e.Text;
                rad.DataSource = (from hv in db.LST_HOCVIENs
                                  where hv.MAHV.Contains(e.Text)
                                  || hv.HODEM.Contains(e.Text)
                                  || hv.TENHV.Contains(e.Text)
                                  || hv.NGAYSINH.ToString().Contains(e.Text)
                                  select new
                                  {
                                      NAME = hv.HODEM + " " + hv.TENHV + " | " + hv.MAHV,
                                      MAHV = hv.MAHV

                                  }).ToList();
            }

            rad.DataBind();

        }

        protected void grvLopHocvien_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string recid = item.OwnerTableView.DataKeyValues[item.ItemIndex]["RECID"].ToString();
            BVVDDataContext db = new BVVDDataContext();
            F_LOP_HOCVIEN record = (from lh in db.F_LOP_HOCVIENs
                                    where lh.RECID.ToString().Equals(recid)
                                    select lh).SingleOrDefault();
            //string mahv = record.LST_HOCVIEN.HODEM + " " + record.LST_HOCVIEN.TENHV;
            //string malh = record.LST_LOPHOC.TENLH;
            db.F_LOP_HOCVIENs.DeleteOnSubmit(record);
            try
            {
                db.SubmitChanges();
                //rnAlert.Text = "Xóa thành công học viên  : " + mahv + " khỏi danh sách học viên của lớp " + malh;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n " + ex.Message;
            }

            rnAlert.Show();
        }




    }
}